package com.wzy.server.util;


//FastDFS工具类

import org.csource.fastdfs.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;


public class FastDFSUtils {
    private static Logger logger= LoggerFactory.getLogger(FastDFSUtils.class);//日志

//    初始化客户端
    static {
        try {
            String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();//获取配置文件路径
            ClientGlobal.init(filePath);//加载fastDFS客户端的配置文件
        } catch (Exception e) {
            logger.error("初始化FastDFS失败",e.getMessage());
        }
    }

//创建tracker的服务器
    private static TrackerServer getTrackerServer() throws IOException {
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getConnection();//tracker.getTrackerServer()
        return  trackerServer;
    }

//创建的服务器Storage客户端
    private static StorageClient getStorageClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        StorageServer storageServer = null;
        StorageClient storageClient = new StorageClient(trackerServer,storageServer);
        return storageClient;
    }

//上传文件
    public static String[] upload(MultipartFile file){
        String name = file.getOriginalFilename();//文件的名字
        logger.info("文件名",name);//打印文件名
        StorageClient storageClient = null;
        String[] uploadResults =null;
        try {
            storageClient = getStorageClient();//获取Storage客户端
            uploadResults = storageClient.upload_file(file.getBytes(), name.substring(name.lastIndexOf(".") + 1), null);//上传
        } catch (Exception e) {
            logger.error("上传文件失败",e.getMessage());
        }
        if (null == uploadResults){
            logger.error("上传文件失败",storageClient.getErrorCode());
        }
        return uploadResults;
    }

//    获取文件信息
    public static FileInfo getFileInfo(String groupName,String remoteFileName){
        StorageClient storageClient = null;
        try {
            storageClient = getStorageClient();//获取Storage客户端
            return storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
            logger.error("文件信息获取失败",e.getMessage());
        }
        return null;
    }

    //   下载文件
    public static InputStream downFile(String groupName, String remoteFileName){
        StorageClient storageClient = null;
        try {
            storageClient = getStorageClient();//获取Storage客户端
            byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(fileByte);//流
            return inputStream;
        } catch (Exception e) {
            logger.error("文件下载失败",e.getMessage());
        }
        return null;
    }

    //   删除文件
    public static void deleteFile(String groupName, String remoteFileName){
        StorageClient storageClient = null;
        try {
            storageClient = getStorageClient();//获取Storage客户端
            storageClient.delete_file(groupName,remoteFileName);
        } catch (Exception e) {
            logger.error("文件删除失败",e.getMessage());
        }
    }

//    获取文件路径
    public static String getTrackerUrl(){
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer =null;
        StorageServer storeStorage  = null;
        try {
            trackerServer = trackerClient.getConnection();
            storeStorage = trackerClient.getStoreStorage(trackerServer);
        } catch (Exception e) {
            logger.error("文件路径获取失败",e.getMessage());
        }
        return "http://"+storeStorage.getInetSocketAddress().getHostString()+":8888/";
    }

}
